home *** CD-ROM | disk | FTP | other *** search
/ Pascal Super Library / Pascal Super Library (CW International)(1997).bin / LIBRARY / PAS_0793 / FIREWORK.PAS < prev    next >
Pascal/Delphi Source File  |  1993-08-01  |  8KB  |  396 lines

  1. {─ Fido Pascal Conference ────────────────────────────────────────────── PASCAL ─
  2. Msg  : 365 of 375
  3. From : Rick Saunooke                       1:3641/1.0           01 Jul 93  23:33
  4. To   : All
  5. Subj : A Contribution - Part 3
  6. ────────────────────────────────────────────────────────────────────────────────}
  7. program Fire_Works;
  8. uses TPcrt, colour;
  9.  
  10. const
  11.  sh=16;
  12.  mul=65536;
  13.  csize=64;
  14.  works: string[30] = 'kablooie.kab';
  15.         VGA_Segment     = $0A000;
  16.  
  17. Type
  18.  ppack=^pack;
  19.  pload=^load;
  20.  pack=record
  21.   number   : integer;         { * Quantity Of Pixels * }
  22.   variance : integer;         { * Deviation From Number * }
  23.   stuff: pload;
  24.   boost: integer;
  25.   next: ppack;
  26.  end;
  27.  load=record
  28.   name: string[30];
  29.   flash: boolean;
  30.   cont: ppack;
  31.   cset: byte;
  32.   decay: byte;
  33.   gravity : longint;
  34.   next: pload;
  35.   gnext: pload;
  36.  end;
  37.  ppix=^pix;
  38.  pix=record
  39.   x,y,dx,dy: longint;
  40.   k: byte;
  41.   l: pload;
  42.   last,next: ppix;
  43.  end;
  44.  
  45. var
  46.  disp: ppix;
  47.  batt: pload;
  48.  parts: pload;
  49.  count,loads: integer;
  50.  sina,cosa: array[0..360] of longint;
  51.  test,spark: load;
  52.  testpack: pack;
  53.  launch: pix;
  54.  maxx,maxy: integer;
  55.  f: text;
  56.         DFactor : Integer;
  57.  
  58. { ************************************************************************* }
  59. procedure click;assembler;
  60. asm
  61.  in al,$61;
  62.  xor al,2;
  63.  out $61,al;
  64. end;
  65. { ************************************************************************* }
  66. Procedure VideoMode ( VMode : Byte );
  67. Begin { VideoMode }
  68.   Asm
  69.     Mov  AH,00
  70.     Mov  AL,VMode
  71.     Int  10h
  72.   End;
  73. End;  { VideoMode }
  74. { ************************************************************************* }
  75. procedure Put_Pixel(x,y,c: integer);
  76. begin { proc PutPixel }
  77.     Mem[$A000:word(320*y+x)]:=c;
  78. end; { proc PutPixel }
  79. { ************************************************************************* }
  80. procedure addpix(d: pix);
  81. var p: ppix;
  82. begin
  83.  new(p);
  84.  p^:=d;
  85.  p^.last:=nil;
  86.  p^.next:=disp;
  87.  if disp<>nil then
  88.   disp^.last:=p;
  89.  disp:=p;
  90.  inc(count);
  91. end;
  92.  
  93. procedure rempix(p: ppix);
  94. begin
  95.  if p^.last<>nil then
  96.   p^.last^.next:=p^.next
  97.  else
  98.   disp:=p^.next;
  99.  if p^.next<>nil then
  100.   p^.next^.last:=p^.last;
  101.  dispose(p);
  102.  dec(count);
  103. end;
  104.  
  105. procedure gentrig;
  106. var i: integer;
  107. begin
  108.  for i:=0 to 360 do
  109.  begin
  110.   cosa[i]:=round(cos(pi*i/180)*mul);
  111.   sina[i]:=round(sin(pi*i/180)*mul);
  112.  end;
  113. end;
  114.  
  115. procedure initpix(from: ppix);
  116. var i: integer;
  117.   p: pix;
  118.   th: integer;
  119.   pp: ppack;
  120. begin
  121.  with from^,from^.l^ do
  122.  begin
  123.   if flash then
  124.   begin
  125.                   Click;
  126.    assigncolor(0,white);
  127.    assigncolor(1,grey90);
  128.   end;
  129.   p.x:=x;
  130.   p.y:=y;
  131.   pp:=cont;
  132.   while pp<>nil do
  133.   with pp^ do
  134.   begin
  135.    p.l:=stuff;
  136.    for i:=1 to number+random(variance+1)*2-variance do
  137.    with p do
  138.    begin
  139.     k:=random(p.l^.decay);
  140.     th:=random(360);
  141.     dx:=round(cosa[th]*k*boost/p.l^.decay)+from^.dx;
  142.     dy:=round(sina[th]*k*boost/p.l^.decay)+from^.dy;
  143.     addpix(p);
  144.    end;
  145.    pp:=pp^.next;
  146.   end;
  147.   if flash then
  148.   begin
  149.                         Click;
  150.    assigncolor(0,black);
  151.    assigncolor(1,grey10);
  152.   end;
  153.  end;
  154. end;
  155.  
  156. procedure fire;
  157. var i: integer;
  158. begin
  159.   With launch do
  160.  begin
  161.   x:=longint(random(maxx)) shl sh;
  162.   y:=longint(maxy) shl sh;
  163.   if x>longint(maxx) shl (sh-1) then
  164.    dx:=-round(random*mul)
  165.   else
  166.    dx:=round(random*mul);
  167.   dy:=longint(-5)*mul;
  168.   l:=batt;
  169.   for i:=1 to random(loads) do
  170.    l:=l^.next;
  171.   k:=0;
  172.   addpix(launch);
  173.  end;
  174. end;
  175.  
  176. procedure disppix;
  177. var p,  q  : ppix;
  178.     xl, yl : longint;
  179.        RMS : Integer;
  180. begin
  181.   xl:=longint(maxx) shl sh;
  182.   yl:=longint(maxy) shl sh;
  183.   p:=disp;
  184.   while p<>nil do
  185.  with p^,p^.l^ do
  186.  begin
  187.        q:=p^.next;
  188.    Put_Pixel (x shr sh,y shr sh,1);
  189.    inc (x,dx*maxx div 640);
  190.    inc (y,dy*maxy div 480);
  191.    inc (dy,gravity);
  192.    inc (k);
  193.    if (k=decay) or (x<0) or (x>xl) or (y<0) or (y>yl) then
  194.    begin
  195.      if (x>0) and (x<xl) and (y>0) and (y<yl) then
  196.         initpix(p);
  197.         rempix(p);
  198.    end else
  199.    Put_Pixel (x shr sh,y shr sh,(integer(k)*csize div decay)+cset*csize);
  200.    p:=q;
  201.  end;
  202.  delay (DFactor);
  203.         RMS:=Random(60);
  204.         IF count=0 then
  205.         Begin
  206.           DFactor:=12;
  207.           Fire;
  208.         End;
  209.         Case Count Of
  210.                0..49  : DFactor:=11;
  211.               50..99  : DFactor:=10;
  212.              101..119 : DFactor:=9;
  213.              120..141 : DFactor:=8;
  214.              140..161 : DFactor:=7;
  215.              160..181 : DFactor:=6;
  216.              180..201 : DFactor:=5;
  217.              200..250 : DFactor:=4;
  218.              251..300 : DFactor:=3;
  219.              301..325 : DFactor:=2;
  220.              326..350 : DFactor:=1;
  221.              351..999 : DFactor:=0;
  222.         End;
  223.         IF Odd(RMS) THEN
  224.         Begin
  225.           If Count=60 Then
  226.              Fire
  227.         End
  228.         Else If Count=30 Then
  229.                 Fire;
  230. end;
  231.  
  232. procedure init;
  233. var o, m, mm, i : integer;
  234.     s           : string;
  235.     col         : palette;
  236. begin
  237.   randomize;
  238.   asm
  239.     mov ax,$13
  240.     int $10
  241.   end;
  242.   gentrig;
  243.   maxx:=320;
  244.   maxy:=200;
  245.   count:=0;
  246.   loads:=0;
  247.   col[1]:=grey10;
  248.   col[2]:=white;
  249.   col[csize-1]:=orange;
  250.   range(col,2,csize-1);
  251.   col[csize]:=white;
  252.   col[2*csize-1]:=blue;
  253.   range(col,csize,2*csize-1);
  254.   col[2*csize]:=white;
  255.   col[3*csize-1]:=red;
  256.   range(col,2*csize,3*csize-1);
  257.   col[3*csize]:=white;
  258.   col[4*csize-1]:=jade;
  259.   range(col,3*csize,4*csize-1);
  260.   setcolors(col,1,1,4*csize-1);
  261.   disp:=nil;
  262.   parts:=nil;
  263. end;
  264.  
  265. procedure addload(l: load);
  266. var p: pload;
  267. begin
  268.   new(p);
  269.   p^:=l;
  270.   p^.next:=batt;
  271.   p^.cont:=nil;
  272.   batt:=p;
  273.   p^.gnext:=parts;
  274.   parts:=p;
  275.   inc(loads);
  276. end;
  277. procedure addpart(l: load);
  278. var p: pload;
  279. begin
  280.   new(p);
  281.   p^:=l;
  282.   p^.next:=nil;
  283.   p^.gnext:=parts;
  284.   p^.cont:=nil;
  285.   parts:=p;
  286. end;
  287. procedure addpack(l: pload; k: pack);
  288. var p: ppack;
  289. begin
  290.   new(p);
  291.   p^:=k;
  292.   p^.next:=l^.cont;
  293.   l^.cont:=p;
  294. end;
  295.  
  296. function findload(n: string): pload;
  297. var p: pload;
  298. begin
  299.   p:=parts;
  300.   n:=copy(n,1,30);
  301.   while (p<>nil) and (p^.name<>n) do
  302.         p:=p^.gnext;
  303.   findload:=p;
  304. end;
  305.  
  306. {$I-}
  307. procedure err(s: string);
  308. begin
  309.   VideoMode(3);
  310.   CLRSCR;
  311.   Writeln('Error in file: ',works,': ',s);
  312.   close(f);
  313.   halt(1);
  314. end;
  315.  
  316. function getstr: string;
  317. var s : string;
  318.     i : integer;
  319. begin
  320.   s:='';
  321.   while ((s='') or (s[1]=' ')) and (not eof(f)) do
  322.   begin
  323.     readln(f,s);
  324.   end;
  325.   if (s='') or (s[1]=' ') then
  326.      err('premature EOF.');
  327.   for i:=1 to length(s) do
  328.       s[i]:=upcase(s[i]);
  329.   getstr:=s;
  330. end;
  331.  
  332. function getnum: integer;
  333. var n,nn: integer;
  334. begin
  335.  val(getstr,n,nn);
  336.  if nn<>0 then
  337.   err('invalid number.');
  338.  getnum:=n;
  339. end;
  340. procedure readfile;
  341. var l: load;
  342.   p: pack;
  343.   s,t: string;
  344. begin
  345.  assign(f,works);
  346.  reset(f);
  347.  if ioresult<>0 then
  348.   err('cannot be opened.');
  349.  while not eof(f) do
  350.  begin
  351.   s:=getstr;
  352.   if (s<>'FIREWORK') and (s<>'PACKING') then
  353.    err('FIREWORK or PACKING expected.');
  354.   l.name:=getstr;
  355.   t:=getstr;
  356.   if (t<>'FLASH') and (t<>'NOFLASH') then
  357.    err('FLASH or NOFLASH expected.');
  358.   l.flash:=(t='FLASH');
  359.   t:=getstr;
  360.   if (t<>'RED') and (t<>'YELLOW') and (t<>'GREEN') and (t<>'BLUE') then
  361.    err('color name expected.');
  362.   if t='RED' then l.cset:=2;
  363.   if t='YELLOW' then l.cset:=0;
  364.   if t='GREEN' then l.cset:=3;
  365.   if t='BLUE' then l.cset:=1;
  366.   l.decay:=getnum;
  367.   l.gravity:=getnum;
  368.   l.gravity:=l.gravity*mul div 100;
  369.   if s='FIREWORK' then addload(l) else addpart(l);
  370.   s:=getstr;
  371.   while s<>'END' do
  372.   begin
  373.    p.stuff:=findload(s);
  374.    if p.stuff=nil then
  375.     err('packing not found.');
  376.    p.number:=getnum;
  377.    p.variance:=getnum;
  378.    p.boost:=getnum;
  379.    addpack(parts,p);
  380.    s:=getstr;
  381.   end;
  382.  end;
  383.  close(f);
  384. end;
  385. {$I+}
  386.  
  387. begin
  388.         DFactor:=12;
  389.  init;
  390.  readfile;
  391.  repeat
  392.            while not keypressed do
  393.           disppix;
  394.  until readkey=#27;
  395.         videoMode (3);
  396. end.